/**
菜单
 kaige @2022
 */
layui.define(['layer', 'dropdown', 'util', 'element'], function (exports) {
  "use strict";
  var $ = layui.jquery, layer = layui.layer, dropdown = layui.dropdown, util = layui.util, element = layui.element;
  var b = function () { };

  b.prototype.view_leftmenu = function () {
    var that = this;
    var obj = $("#nav_left_menu");
    var d = kaige_page.leftmenu;
    var s = "";
    if (d && d.length < 1) {
      layer.msg('无数据');
      return;
    }
    for (var i = 0; i < d.length; i++) {
      s += '<li title="' + d[i].title + '" data-menu="id:' + d[i].id + ',openType:' + d[i].openType + ',url:' + d[i].pageURL + ',width:' + d[i].width + ',height:' + d[i].height + ',name:' + d[i].name + ',icon:' + d[i].icon + ',title:' + d[i].title + '"><span>' + this.getIconHtml(d[i].icon) + '</span><span>' + d[i].name + '</span></li>';
    }
    obj.html(s);
    obj.find('li').on('click', function (e) {
      $('.sel-leftmenu').removeClass('sel-leftmenu');
      $(this).addClass('sel-leftmenu');
      var menustr = $(this).attr('data-menu');
      if (!menustr || menustr.indexOf('\'') > -1 || menustr.indexOf('"') > -1) {
        layer.tips('菜单配置错误！', menuItem, { time: 1000 });
        return false;
      }
      // 处理菜单，得到JSON。何不不用JSON.parse，因双引号烦人。
      var menuData = {};
      var menuarr = menustr.split(',');
      $.each(menuarr, function (i1, item) {
        var tmparr = item.split(':');
        menuData[tmparr[0]] = tmparr[1] ? tmparr[1] : '';
      });
      // console.log(menuData);
      if (menuData.openType == 1) {
        var area = ['95%', '85%'];
        if (menuData.width && menuData.height) {
          area = [menuData.width, menuData.height];
        }
        layer.open({
          id: 'ow_' + menuData.id,
          type: 2,
          title: menuData.name,
          content: menuData.url,
          area: area,
          offset: 'auto',
          anim: 0,
          shade: 0,
          moveOut: false,  //允许拖出窗外
        });
      } else {
        that.view_right(menuData);
      }
    });
  };

  b.prototype.getIconHtml = function (jxicon) {
    var iconhtml = '';
    if (jxicon) {
      if (jxicon.indexOf('fa-') > -1) { // 支持fa系列
        iconhtml = '<i class="fa ' + jxicon + ' "></i>';
      } else if (jxicon.indexOf('icon-') > -1) {// 支持iconfont 系列
        iconhtml = '<i class="iconfont ' + jxicon + '"></i>';
      } else if (jxicon.indexOf('layui-icon-') > -1) {// layui-icon 系列
        iconhtml = '<i class="layui-icon ' + jxicon + '"></i>';
      } else {
        iconhtml = '';
      }
    }
    return iconhtml;
  };

  b.prototype.getAhtml = function (adata, level) {
    var that = this, shtml = '', datamenu = '';
    if (adata.childs == null) {
      datamenu = 'data-menu="id:' + adata.id +
        ',url:' + adata.pageURL +
        ',title:' + adata.name +
        (adata.openType ? ',opentype:' + adata.openType : '') +
        (adata.maxopen ? ',maxopen:' + adata.maxopen : '') +
        (adata.icon ? ',icon:' + adata.icon : '') +
        (adata.width ? ',width:' + adata.width : '') +
        (adata.height ? ',height:' + adata.height : '') +
        '"';
    }
    shtml += '<a win-id="' + adata.id + '" ' + (adata.childs != null ? 'iaparent=1' : 'iaparent=0') + ' style="padding-left:' + (level * 10) + 'px" ' + datamenu + ' >';
    shtml += '<span class="ml-menu-name" data-text="' + adata.title + '">' + that.getIconHtml(adata.icon) + ' ' + adata.name + '</span>';
    if (adata.childs != null) {
      shtml += '<i class="layui-icon layui-icon-down layui-nav-more"></i>';
    }
    shtml += '</a>';
    return shtml;
  };
  b.prototype.getChild = function (sub, level) { // 无限子级渲染
    var that = this, shtml = '';
    shtml = (level === 1 ? '' : '<dl class="layui-nav-child">');
    $(sub).each(function (i9, e9) {
      if (e9.childs != null || level === 1) { // 有子项
        shtml += '<' + (level === 1 ? 'li' : 'dd') + ' class="layui-nav-item' + ' ' + (e9.childs != null ? 'parent' : '') + ' ' + (e9.extend == 'true' ? ' layui-nav-itemed' : '') + '">';
        shtml += that.getAhtml(e9, level);
        if (level > 1 || (level === 1 && e9.childs != null)) {
          shtml += that.getChild(e9.childs, level + 1); // 递归
        }
        shtml += '</' + (level === 1 ? 'li' : 'dd') + '>';
      } else { // 无子项
        shtml += (level === 1 ? that.getAhtml(e9, level) : '<dd>' + that.getAhtml(e9, level) + '</dd>');
      }
    });
    shtml += (level === 1 ? '' : '</dl>');
    return shtml;
  };

  b.prototype.aclick = function () {
    var that = this;
    $('[win-id]').on('click', function (e) {
      that.openTab(this);
    });
  };

  b.prototype.openTab = function (menuItem) {
    var $this = $(menuItem);
    // 兼容win-id
    if ($this.length < 1) {
      $this = $('[win-id="' + menuItem + '"]');
    }

    // 拦截有子项
    var isParent = $this.attr('iaparent');
    if (isParent == 1) return false;
    // 必须存在 data-menu
    var menustr = $this.attr('data-menu'); // 字符串中不能含 单引号或双引号 格式例：a:1,b2:abcd,c:false
    if (!menustr || menustr.indexOf('\'') > -1 || menustr.indexOf('"') > -1) {
      layer.tips('菜单配置错误！', menuItem, { time: 1000 });
      return false;
    }
    // 处理菜单，得到JSON。何不不用JSON.parse，因双引号烦人。
    var menuData = {};
    var menuarr = menustr.split(',');
    $.each(menuarr, function (i1, item) {
      var tmparr = item.split(':');
      menuData[tmparr[0]] = tmparr[1] ? tmparr[1] : '';
    });

    var url = menuData.url,
      title = menuData.title || menuData.name,
      id = $this.attr('win-id') || menuData.id,
      type = menuData.opentype ? parseInt(menuData.opentype) : 2,
      maxOpen = menuData.maxopen ? parseInt(menuData.maxopen) : -1,
      icon = menuData.icon,
      w_width = menuData.width ? menuData.width : 0,
      w_height = menuData.height ? menuData.height : 0,
      area = ['90%','80%'];

    if (w_width && w_height) {
      area[0] = typeof (w_width) === "number" ? w_width + 'px' : w_width;
      area[1] = typeof (w_height) === "number" ? w_height + 'px' : w_height;
    }

    // 若为空
    if (!url || !title || !id) {
      layer.tips('菜单配置错误！', menuItem, { tips: 1 });
      return false;
    }

    if (type == 1) {
      layui.oawin.open({
        id: id,
        title: title,
        icon: icon,
        content: (szoa.client.path || '') + url,
        area: area,
      });
      return;
    }

    layui.oawin.addTabs({
      id: id,
      title: title,
      icon: icon,
      url: (szoa.client.path || '') + url
    });
  };

  //渲染右边
  b.prototype.view_right = function (opt) {
    var that = this, s = '', data = kaige_page.menus;
    if (data && data.length < 1) {
      layer.msg('无数据');
      return;
    }
    if (!opt) {
      opt = data[data.length - 1];
    }
    s = '<li class="layui-nav-item"><a class="ml-header" style="margin-left: 10px;">' + that.getIconHtml(opt.icon) + ' ' + opt.name + '</a></li>'; // 头
    // 读取数据
    $(data).each(function (i9, e9) {
      //只取当前模块下的数据
      if (e9.id == opt.id) {
        //渲染
        s += that.getChild(e9, 1);
      }
    });
    $('.ml-menu').html(s);
    element.render('nav');
    that.aclick();
  };


  b.prototype.init = function () {
    this.view_leftmenu();
    this.view_right();//默认个人，写固定
  };

  var oamenu = new b();
  exports('oamenu', oamenu);
});